import React from 'react';
import { observer, inject } from "mobx-react";
import MemberSelectModal from '../../components/MemberSelectModal';

@inject(({ contactsState }) => ({ contactsState }))
@observer
class HandleMemberSelect extends React.Component {

    onSearch = (params) => {
        const { getList } = this.props.contactsState;
        getList(params);
    }

    handleOk = ({ targets, targetsIds }, callback) => {
        const { onOk } = this.props;
        onOk({ targets, targetsIds }, callback);
    }

    render() {
        const { show, onCancel, members = [], loading, isAdd, title } = this.props;
        const { contacts } = this.props.contactsState;

        let dataSource = [];
        if (isAdd) dataSource = contacts.filter(val => !members.some(v => v.userid === val.userid)).map(v => ({ ...v, username: v.realname }));
        else dataSource = members.filter(v => !v.ishost && !v.ismanager); // 管理人和主办人不可以删除

        return (
            <MemberSelectModal
                title={title}
                isAdd={isAdd}
                loading={loading}
                visible={show}
                mainKey={'userid'}
                showKey={'username'}
                showAvatar={true}
                onSearch={this.onSearch}
                onOk={this.handleOk}
                onCancel={onCancel}
                dataSource={dataSource}
            />
        );
    }
}

export default HandleMemberSelect